#include "db.h"
#include "logger.h"
#include "threadLock.h"
#include <stdio.h>
#include <string.h>




sqlite3* g_db = NULL;

static T_THREAD_LOCK g_db_lock = {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, PTHREAD_MUTEX_RECURSIVE};

/**
 * @brief 打开数据库
 */
int db_open(const char* db_path){

    if((NULL == db_path) || (1 > strlen(db_path))){
        return -1;
    }
    threadLockLock(&g_db_lock);
    if (sqlite3_open(db_path, &g_db) != SQLITE_OK) {
        log_e("Can't open database: %s", sqlite3_errmsg(g_db));
        sqlite3_close(g_db);
        threadLockUnlock(&g_db_lock);
        return -2;
    }
    threadLockUnlock(&g_db_lock);
    return 0;
}
/**
 * @brief 关闭数据库
 */
int db_close(){
    if(NULL == g_db){
        return -1;
    }
    threadLockLock(&g_db_lock);
    if (sqlite3_close(g_db) != SQLITE_OK) {
        log_e("Can't close database: %s", sqlite3_errmsg(g_db));
        threadLockUnlock(&g_db_lock);
        return -1;
    }
    threadLockUnlock(&g_db_lock);
    return 0;
}
/**
 * @breif 插入数据到数据库
 */
int db_insert(const char* sql){
    if((NULL == sql) || (1 > strlen(sql))){
        return -1;
    }
    threadLockLock(&g_db_lock);
    if (sqlite3_exec(g_db, sql, NULL, NULL, NULL) != SQLITE_OK) {
        log_e("Can't insert(%s) data: %s", sql, sqlite3_errmsg(g_db));
        threadLockUnlock(&g_db_lock);
        return -1;
    }
    threadLockUnlock(&g_db_lock);
    return 0;
}
/**
 * @breif 覆盖数据到数据库
 */
int db_replace(const char* sql){
    if((NULL == sql) || (1 > strlen(sql))){
        return -1;
    }
    threadLockLock(&g_db_lock);
    if (sqlite3_exec(g_db, sql, NULL, NULL, NULL) != SQLITE_OK) {
        log_e("Can't replace(%s) data: %s", sql, sqlite3_errmsg(g_db));
        threadLockUnlock(&g_db_lock);
        return -1;
    }
    threadLockUnlock(&g_db_lock);
    return 0;
}
/**
 * @breif 删除数据
 */
int db_delete(const char* sql){
    if((NULL == sql) || (1 > strlen(sql))){
        return -1;
    }
    threadLockLock(&g_db_lock);
    if (sqlite3_exec(g_db, sql, NULL, NULL, NULL) != SQLITE_OK) {
        log_e("Can't replace(%s) data: %s", sql, sqlite3_errmsg(g_db));
        threadLockUnlock(&g_db_lock);
        return -1;
    }
    threadLockUnlock(&g_db_lock);
    return 0;
}
/**
 * @brief 创建数据表
 */
int db_create_table(const char* sql){
    if((NULL == sql) || (1 > strlen(sql))){
        return -1;
    }
    threadLockLock(&g_db_lock);
    if (sqlite3_exec(g_db, sql, NULL, NULL, NULL) != SQLITE_OK) {
        log_e("Can't create(%s) table: %s", sql, sqlite3_errmsg(g_db));
        threadLockUnlock(&g_db_lock);
        return -1;
    }
    threadLockUnlock(&g_db_lock);
    return 0;
}

int db_select(const char* sql, T_DB_CALLBACK_FUNC callback, void* data){
    if((NULL == sql) || (1 > strlen(sql)) || (NULL == data)){
        return -1;
    }
    /* Execute SQL statement */
    threadLockLock(&g_db_lock);
    if(sqlite3_exec(g_db, sql, callback, data, NULL) != SQLITE_OK ){
        log_e("Can't select(%s) data: %s", sql, sqlite3_errmsg(g_db));
        threadLockUnlock(&g_db_lock);
        return -1;
    }
    threadLockUnlock(&g_db_lock);
    return 0;
}